<template>
    <div v-if="showPreview" class="preview" @click="closeView">
        <div class="preview-close" @click="closeView"><i class="el-icon-close"></i></div>
        <img class="preivew-image" @click.prevent.stop :src="target" :style="imageStyle"/>
        <div class="preview-options">
            <i @click.prevent.stop="scaleChange('-')" class="el-icon-zoom-out"></i>
            <i @click.prevent.stop="scaleChange" class="el-icon-zoom-in"></i>
            <i v-if="isFullScreen" @click.prevent.stop="fullScreenChange" class="el-icon-c-scale-to-original"></i>
            <i v-else @click.prevent.stop="fullScreenChange" class="el-icon-full-screen"></i>
            <i @click.prevent.stop="rotateChange('-')" class="el-icon-refresh-left"></i>
            <i @click.prevent.stop="rotateChange" class="el-icon-refresh-right"></i>
        </div>
    </div>
</template>

<script>
    export default {
        name: "ImagePreview",
        props: {
            targetClass: {
                type: String,
                default: 'gc-preview'
            }
        },
        data() {
            return {
                showPreview: false,
                isFullScreen: true,
                scale: 1,
                rotate: 0,
                target: "",
            }
        },
        computed: {
            imageStyle() {
                let style = {
                    transform: 'scale(' + this.scale + ') rotate(' + this.rotate + 'deg)',
                    margin: 0,
                    transition: 'transform 0.3s ease 0s'
                };
                if (this.isFullScreen) {
                    style['max-height'] = "100%";
                    style['max-width'] = "100%";
                }
                return style;
            }
        },
        mounted() {
            this.listenDocument();
        },
        methods: {
            listenDocument() {
                var imageClick = (event) => {
                    let tagName = (event.target.nodeName).toLowerCase();
                    if (tagName == 'img') {
                        let tagClassName = event.target.className + "";
                        if (tagClassName.search(this.targetClass) >= 0 || this.checkParent(event.target)) {
                            this.target = event.target.currentSrc;
                            this.showPreview = true;
                        }
                    }
                };
                document.removeEventListener('click', imageClick);
                document.addEventListener('click', imageClick);
            },
            checkParent(dom) {
                let parentDom = dom.parentElement;
                if (parentDom) {
                    if (parentDom.className.search(this.targetClass) >= 0) {
                        return true;
                    } else {
                        return this.checkParent(parentDom);
                    }
                }
                return false;
            },
            closeView() {
                this.showPreview = false;
                this.isFullScreen = true;
                this.scale = 1;
                this.rotate = 0;
            },
            scaleChange(operation) {
                let step = 0.2;
                if (operation == '-') {
                    if (this.scale > step) {
                        this.scale = parseFloat((this.scale - step).toFixed(1));
                    }
                } else {
                    this.scale = parseFloat((this.scale + step).toFixed(1));
                }
            },
            rotateChange(operation) {
                let step = 90;
                if (operation == '-') {
                    this.rotate = parseInt(this.rotate - step);
                } else {
                    this.rotate = parseInt(this.rotate + step);
                }
            },
            fullScreenChange() {
                this.scale = 1;
                this.rotate = 0;
                this.isFullScreen = !this.isFullScreen;
            }
        }
    }
</script>

<style lang="less" scoped>
    .preview {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 10000;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: rgba(0, 0, 0, 0.5);
        
        .preview-close {
            position: absolute;
            right: 20px;
            top: 20px;
            color: #FFFFFF;
            font-size: 24px;
            width: 36px;
            height: 36px;
            background-color: #606266;
            border-radius: 50%;
            text-align: center;
            line-height: 36px;
            z-index: 100;
        }
        
        .preview-options {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            background-color: rgba(#606266, 0.8);
            border-radius: 30px;
            padding: 10px 24px;
            color: #FFFFFF;
            font-size: 24px;
            
            i {
                margin: 0 10px;
            }
        }
    }
</style>
